<?php

namespace app\admin\controller;

use app\common\Helper;

use app\admin\model\Operator;
use think\Request;

class Auth extends Base
{

    /**
     * 登录
     */
    public function doLogin()
    {
        $param = self::getHttpParam();

        if (empty($param->loginName)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'loginName 不能为空');
        }
        if (empty($param->password)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'password 不能为空');
        }

        $loginName = trim($param->loginName);
        $password = trim($param->password);
        $adminInfo = Operator::where(['loginName' => $loginName])->find();
        if(!$adminInfo) {
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, '用户名不存在！');
        }

        if(Helper::passwordEncrypt($password) != $adminInfo->password){
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, '密码错误！');
        }

        // 管理员权限
        // 是否是超级管理员
        if($adminInfo['roleType'] == 1){
            $ruleIds = db('module')
                ->where(['isActive' => 1, 'status' => ['in', '1,2']])
                ->field('id as moduleId, name')
                ->select();
        }else{
            $ruleIds = db('operatormodule')
                ->alias('a')
                ->where(['a.operateId' => $adminInfo['id']])
                ->join('__MODULE__ m', 'm.id = a.moduleId')
                ->field('a.moduleId, m.name')
                ->select();
        }
        $adminInfo['ruleIds'] = $ruleIds;

        //session('ADMIN', $adminInfo);
        cookie('ADMIN', $adminInfo, 3600 * 24 * 365);

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $adminInfo);
    }

    /**
     * 退出
     */
    public function logout()
    {
        //session('ADMIN', null);
        cookie('ADMIN', null);

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '');
    }

    /**
     * 修改密码
     */
    public function setPassword(Request $request)
    {
        $param = $request->param();
        if (empty($param['loginName'])) {
            return show(config('status.ERROR_STATUS'), 'loginName 不能为空', []);
        }
        if (empty($param['oriPassword'])) {
            return show(config('status.ERROR_STATUS'), 'oriPassword 不能为空', []);
        }
        if (empty($param['password'])) {
            return show(config('status.ERROR_STATUS'), 'password 不能为空', []);
        }

        $loginName = trim($param['loginName']);
        $password = trim($param['password']);
        $info = Operator::where(['loginName' => $loginName, 'password' => Helper::passwordEncrypt($param['oriPassword'])])->find();
        if(!$info){
            return show(config('status.ERROR_STATUS'), '登录名或密码错误', []);
        }

        $result = Operator::where(['loginName' => $loginName, 'password' => Helper::passwordEncrypt($param['oriPassword'])])->update(['password' => Helper::passwordEncrypt($password)]);
        if($result === false) {
            return show(config('status.ERROR_STATUS'), '修改密码失败', []);
        }

        return show(config('status.SUCCESS_STATUS'), '修改密码成功', '修改密码成功！');
    }

}